#pragma once

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution
{
public:
    int triangleNumber(vector<int>& nums)
    {
        int maxi = nums.size() - 1;
        sort(nums.begin(), nums.end());
        int left = 0, right = maxi - 1;
        int ret = 0;
        for (int i = maxi; i > 1; --i)
        {
            left = 0, right = i - 1;
            while (right > left)
            {
                if (nums[left] + nums[right] > nums[i])
                {
                    ret += (right - left);
                    --right;
                }
                else
                {
                    ++left;
                }
            }
        }

        return ret;
    }
};